package com.lekang.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.lekang.system.mapper.FloorMapper;
import com.lekang.system.domain.Floor;
import com.lekang.system.service.IFloorService;

/**
 * 楼层Service业务层处理
 * 
 * @author zhihao
 * @date 2025-10-13
 */
@Service
public class FloorServiceImpl implements IFloorService 
{
    @Autowired
    private FloorMapper floorMapper;

    /**
     * 查询楼层
     * 
     * @param id 楼层主键
     * @return 楼层
     */
    @Override
    public Floor selectFloorById(Long id)
    {
        return floorMapper.selectFloorById(id);
    }

    /**
     * 查询楼层列表
     * 
     * @param floor 楼层
     * @return 楼层
     */
    @Override
    public List<Floor> selectFloorList(Floor floor)
    {
        return floorMapper.selectFloorList(floor);
    }

    /**
     * 新增楼层
     * 
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int insertFloor(Floor floor)
    {
        // 校验楼层名称是否已存在
        Floor queryFloor = new Floor();
        queryFloor.setFloorName(floor.getFloorName());
        List<Floor> existingFloors = floorMapper.selectFloorList(queryFloor);
        if (existingFloors != null && !existingFloors.isEmpty()) {
            throw new com.lekang.common.exception.ServiceException("楼层名称已存在");
        }
        return floorMapper.insertFloor(floor);
    }

    /**
     * 修改楼层
     * 
     * @param floor 楼层
     * @return 结果
     */
    @Override
    public int updateFloor(Floor floor)
    {
        // 校验楼层名称是否已存在(排除自己)
        Floor queryFloor = new Floor();
        queryFloor.setFloorName(floor.getFloorName());
        queryFloor.setId(floor.getId()); // 设置ID用于排除自己
        List<Floor> existingFloors = floorMapper.selectFloorList(queryFloor);
        if (existingFloors != null && !existingFloors.isEmpty()) {
            throw new com.lekang.common.exception.ServiceException("楼层名称已存在");
        }
        return floorMapper.updateFloor(floor);
    }

    /**
     * 批量删除楼层
     * 
     * @param ids 需要删除的楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorByIds(Long[] ids)
    {
        return floorMapper.deleteFloorByIds(ids);
    }

    /**
     * 删除楼层信息
     * 
     * @param id 楼层主键
     * @return 结果
     */
    @Override
    public int deleteFloorById(Long id)
    {
        return floorMapper.deleteFloorById(id);
    }


    @Override
    public List<Floor> selectFloorWithAllRelations() {
        // 直接调用Mapper的级联查询方法
        return floorMapper.selectFloorWithAllRelations();
    }

    @Override
    public Floor selectFloorWithAllRelationsById(Long id) {
        // 校验ID非空（可选，根据业务需求添加）
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("楼层ID不能为空且必须为正数");
        }
        // 调用Mapper查询单条全链路数据
        return floorMapper.selectFloorWithAllRelationsById(id);
    }



}
